const glob = require('glob');
const path = require('path');
const devServer = require('./proxy.config');
const aliasConfig = require('./alias.config');
const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV);
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i;
const autoprefixer = require('autoprefixer'); //css自动添加浏览器厂商前缀
const pxtoviewport = require('postcss-px-to-viewport');
const mdPage = require("./src/lib/page");

function resolve(dir) {
  return path.join(__dirname, dir);
}

module.exports = {
  productionSourceMap: !IS_PROD,
  lintOnSave: false,
  runtimeCompiler: true,
  publicPath: mdPage.moduleName ? `/${mdPage.moduleName}/` : `/`,
  outputDir: 'dist/' + mdPage.moduleName,
  pages: mdPage.getPages(),
  chainWebpack: config => {
    for (const [key, value] of Object.entries(aliasConfig.resolve.alias)) {
      config.resolve.alias.set(key, value);
    }
    config.optimization.delete('splitChunks');
    config.module
      .rule('svg')
      .exclude.add(resolve('src/common/imgs/svg')) //注意svg的存储地址
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/common/imgs/svg')) //注意svg的存储地址
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
  },

  configureWebpack: config => {
    if (IS_PROD) {
      config.optimization.minimizer[0].options.terserOptions.compress.warnings = false;
      // config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true;
      config.optimization.minimizer[0].options.terserOptions.compress.drop_debugger = true;
      // config.optimization.minimizer[0].options.terserOptions.compress.pure_funcs = [
      //   'console.log',
      // ];

      // 开启 gzip 压缩
      config.plugins.push(
        new CompressionWebpackPlugin({
          filename: '[path].gz[query]',
          algorithm: 'gzip',
          test: productionGzipExtensions,
          threshold: 10240,
          minRatio: 0.8,
        }),
        // new BundleAnalyzerPlugin(),
      );

      // 公共代码抽离
      // config.optimization = {
      //     splitChunks: {
      //         cacheGroups: {
      //             vendor: {
      //                 chunks: 'all',
      //                 test: /node_modules/,
      //                 name: 'vendor',
      //                 minChunks: 1,
      //                 maxInitialRequests: 5,
      //                 minSize: 0,
      //                 priority: 100
      //             },
      //             common: {
      //                 chunks: 'all',
      //                 test: /[\\/]src[\\/]js[\\/]/,
      //                 name: 'common',
      //                 minChunks: 2,
      //                 maxInitialRequests: 5,
      //                 minSize: 0,
      //                 priority: 60
      //             },
      //             styles: {
      //                 name: 'styles',
      //                 test: /\.(sa|sc|c)ss$/,
      //                 chunks: 'all',
      //                 enforce: true
      //             },
      //             runtimeChunk: {
      //                 name: 'manifest'
      //             }
      //         }
      //     }
      // }
    }
  },

  css: {
    loaderOptions: {
      sass: {
        data: `@import "./src/common/css/variables.scss";`,
      },

      postcss: {
        plugins: [

        ],
      },
    },
  },

  devServer,
};
